#include "stdafx.h"

/*
* 
* Name				Lawrence Bush
* Student Number	660 220 742
* Email				bushl2@rpi.edu, Lawrence_Bush@dps.state.ny.us
* Class				Distributed Algorithms and Systems (Spring 2002)
* Professor			Costas Busch
*
* Final Project	Implement a Lock-Free Linked List (based on Valois' paper)
*
*/
//
//////////////////////////////////////////////////////////////////////

//	Description of Program Files
//
//		File		Purpose
//		----		-------
//		lock.cpp	This file implements the Lock class member functions.
//					It conatains a CriticalSection pointer member variable.
//					It also conatains a constructor, destructor, and an unLock function.
//		
//					This class works with the CriticalSection class that I also created.
//					The Lock constructor sets its internal pointer to a CriticalSection 
//					object that is passed in.  Then it enters the critical section.
//					The destructor uses the internal pointer to leave the critical section.
//					Therefore, to uses this lock, we must pass it a CriticalSection object
//					upon instantiation.  The lock can be directly unlocked using the unLock 
//					member function.  However, the lock is unlocked automatically when 
//					it goes out of scope. 
//		
#include "Lock.h"

// *************************************
// *************************************
// ***                               ***
// ***         Lock Structure        *** 
// ***         Implementation        ***
// ***                               ***
// *************************************
// *************************************

// ******   constructor   ******
// This constructor stores a pointer to and 
// enters the passed in critical section
// object.  This is not a windows critical section.
// It is my own construct that contains a windows 
// critical section.

// Therefore, to enter a critical section,
// you just declare a lock and pass it a critical section
// object.
Lock::Lock(CriticalSection * pCritSect) {
	m_pCritical = pCritSect;
	m_pCritical->Enter();
}

// ******   destructor   ******
// This destructor just calls leave on the 
// critical section object that the member data
// points to.  This is my own critical section construct.

// What this destructor does is leaves the critical
// section whenever the object is deleted.
// This means that if the object goes out of scope,
// the destructor will be called and it will 
// automatically leave the critical section.
Lock::~Lock() {
	//LeaveCriticalSection(m_pCritical);
	m_pCritical->Leave();

}
// This call directly leaves the critical section.
// This is available in case the caller needs to leave
// the critical section earlier than when the object goes out of scope.
// This may sometimes be necessary or benefitial because you
// do not want to stay in the critical section longer than
// you need to because it unecessarily prevents another process from getting in.
void Lock::unLock() {
	//LeaveCriticalSection(m_pCritical);
	m_pCritical->Leave();

}
